var PasswordScores = 0;

function ShowErrorMessage(msg) {
    var msgElm = document.getElementById("ErrorMSG");
    msgElm.innerHTML = '<div id="ErrorDisc">' + msg + '</div>';
}

// Validate Data before sending form
function validateForm() {
    
    var regex = /^[a-zA-Z0-9]{3,30}$/;
    //username
    var x=document.forms["user_reg"]["user"].value;
    if (x==null || x=="") {
        ShowErrorMessage("Username must be filled out");
        return false;
    }    
    if ( ! regex.test(x) ) {
        if (x.length < 3 || x.length > 30) {
            ShowErrorMessage("Username length must be between 3 to 30 characters");
        }
        else {
            ShowErrorMessage("Username contains illegal characters");
        }
        return false;        
    }
    
    //email
    var x=document.forms["user_reg"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
        ShowErrorMessage("Not a valid e-mail address");
        return false;
    }    
    //password
    var x=document.forms["user_reg"]["pass"].value;
    if (x==null || x=="") {
        ShowErrorMessage("Password must be filled out");
        return false;
    }
    var y=document.forms["user_reg"]["pass2"].value;
    if (x != y) {
        ShowErrorMessage("Password confirmation error, please enter password again");
        return false;
    }
    if ( PasswordScores < 4 ) {
        ShowErrorMessage("Password is too weak. Try to get at least 'OK' score at the password black box.<br>Hint: long and special password.");
        return false;        
    }
    //nickname
    var regex = /^[a-zA-Z0-9 ]{3,20}$/;
    var x=document.forms["user_reg"]["nick"].value;
    if (x==null || x=="") {
        ShowErrorMessage("Nickname name must be filled out");
        return false;
    }    
    if ( ! regex.test(x) ) {
        if (x.length < 3 || x.length > 20) {
            ShowErrorMessage("Nickname length must be between 3 to 20 characters");
        }
        else {
            ShowErrorMessage("Nickname contains illegal characters");
        }
        return false;        
    }    
}

// avatar swap image by combobox
function swapImage(){
        var image = document.getElementById("imageToSwap");
        var dropd = document.getElementById("avatar_combo");
        image.src = "images/avatars/" + dropd.value + ".png";  
 }

// password strength box
function passwordStrength(password) {
	var desc = new Array();
	desc[0] = "Password not entered";
	desc[1] = "Very Weak Password";
        desc[2] = "Still Weak..";
	desc[3] = "Getting Better..";
	desc[4] = "OK";
	desc[5] = "Strong Password";
	desc[6] = "Strongest Password EVER!!";

        // score calc
	var score   = 0;

	//pas bigger than 2
	if (password.length > 2) score++;

	//pss bigger than 5
	if (password.length > 5) score++;

	//lower and uppercase
	if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;

	//any number
	if (password.match(/\d+/)) score++;

	//any special caracther
	if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) )	score++;

	//bigger than 12
	if (password.length > 10) score++;

        if ( (password.length > 0) && (score < 1) ) score++;

        PasswordScores = score;

	document.getElementById("passwordDescription").innerHTML = "<div id=pass_color_" + score +">" + desc[score] + "</div>";
}    